home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / lkbackup.zip / tape.c < prev    next >
C/C++ Source or Header  |  1993-10-23  |  79KB  |  2,776 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <io.h>
  6. #include <time.h>
  7. #include <process.h>
  8. #include <string.h>
  9. #include <direct.h>
  10. #include <errno.h>
  11.  
  12. /* these are the tape functions for my dat drive */
  13.  
  14.                     
  15.  struct _TAPE_SET_MEDIA_PARAMETERS tparms;
  16.  
  17.  
  18.  #define max_save_sets 50
  19.  #define save_sets_per_block 10
  20.  #define blocks_per_header 5
  21.  #define bsize1 4096
  22.  const int blocksize = bsize1;
  23.  DWORD buffersize = bsize1;
  24.  const int headersize = 2048;
  25.  char saveset_buffer[2049];
  26.  BOOLEAN debug = FALSE;
  27.  int save_sets_on_tape = 0;
  28.  static char backup_version[13] = "LGKBACKUP1.0\0";
  29.  unsigned int total_original_size;
  30.  unsigned int total_backup_size;
  31.  int current_tape_block_number = 0;
  32.  BOOLEAN compress = FALSE;
  33.  int filescompressed = 0;
  34.  int filesuncompressed = 0;
  35.  DWORD start_time;
  36.  
  37.  typedef  struct savesettag {
  38.             char filename[300];
  39.             unsigned int filesize; /* suports 4 gif */
  40.             unsigned int starting_datablock_number;
  41.            } SAVESET_HEADER;
  42.            
  43.  SAVESET_HEADER my_saveset_header;
  44.  
  45.  
  46.  
  47. /* 11 per buffer use 4 buffers */
  48.  
  49.  typedef struct tapeheadertag {
  50.                   char saveset_name[18];
  51.                   unsigned int original_size;
  52.                   unsigned int compressed_size;
  53.                   char date[9];
  54.                   } TAPE_HEADER;
  55.                   
  56.   TAPE_HEADER my_tape_header;
  57.  
  58.   typedef struct rawtag {
  59.             char backupversion[13];
  60.             TAPE_HEADER header[max_save_sets];
  61.             } RAW_TAPE_HEADERT;
  62.  
  63. /* 15 save sets means we only need the first 512 byte block
  64.  since each reacord needs 32 bytes plus the first 12 = 502 total */
  65.  
  66.  
  67.  RAW_TAPE_HEADERT raw_tape_header;
  68.     
  69.     typedef struct indexrectag {
  70.                   int original_size;
  71.                  int compressed_size;
  72.                  char fname[351];
  73.                  int start_block;
  74.                  char date[9];
  75.                     } INDEX_REC_TYPE;
  76.   
  77.  
  78.  const int header_size = sizeof(raw_tape_header);
  79.   
  80.   
  81.   FILE *TempIndexFile;
  82.   char TempIndexFileName[MAX_PATH];
  83.   BOOLEAN tempindexfileopen = FALSE; 
  84.   HANDLE hTape;   /* handle to tape device */
  85.   DWORD  dwBytesRead, dwBytesWritten, dwPos;
  86.   BOOLEAN rdflag, wrflag;
  87.   BOOLEAN tape_opened = FALSE;
  88.   char todays_date[9];
  89.   unsigned int total_bytes_read = 0;
  90.   BOOLEAN quiet_mode = FALSE;
  91.   BOOLEAN set_mark_positioning = FALSE;
  92.   BOOLEAN reading_header = FALSE;
  93.   int filesskipped = 0;
  94.  
  95. #define severe 1
  96. #define warning 0
  97.  
  98.  /* ------------------------------------------------------------------------------------ */
  99.  
  100. /* beginning of functions */
  101. /* ------------------------------------------------------------------------------------------- */
  102.    /* recursive wild card search routine */
  103.  
  104.    BOOLEAN wild_match(search_string,target)
  105.      char *search_string;
  106.      char *target;
  107.  
  108.      {
  109.  
  110.       char *new_target;
  111.       int lens = strlen(search_string);
  112.       int lent = strlen(target);
  113.       BOOLEAN rvalue = FALSE;
  114.  
  115.  
  116.       /* handle terminating cases first */
  117.       if (lens == 1) /* last char of search string */
  118.         {
  119.           if (strncmp(search_string,"*",1) == 0) 
  120.             {
  121.               /* is a wild card * */
  122.               return(TRUE);
  123.             }
  124.           
  125.           else if ((strncmp(search_string,"?",1) == 0) && (lent == 1))
  126.              {
  127.                /* is a wild card ? */
  128.                return(TRUE);
  129.              }
  130.           else /* other char only matches if lent is also 1 and they match */
  131.              {
  132.                if ((lent == 1) && (strncmp(search_string,target,1) == 0))  
  133.                  return(TRUE);
  134.                else return(FALSE);
  135.              }
  136.  
  137.          } /* lens = 1 */
  138.         
  139.        else /* lens > 1 */
  140.          {
  141.            /* now check if chars is ? */
  142.            if (strncmp(search_string,"?",1) == 0)
  143.              {
  144.                /* if ? skip a char in both strings and recurse */
  145.                ++search_string;
  146.                ++target;
  147.                return(wild_match(search_string,target));
  148.              }
  149.               /* now check if char is * */
  150.             if (strncmp(search_string,"*",1) == 0)
  151.               {
  152.                      /* find next char in search string  */
  153.                      do
  154.                         {
  155.                          ++search_string;       
  156.                            /* here we also need to skip over ? as long as there is a char left
  157.                              in the target */
  158.                               
  159.                         } while (((strncmp(search_string,"*",1) == 0) || 
  160.                                   ((strncmp(search_string,"?",1) == 0)) && (lent >= 1)));
  161.                        /* if it is a ? recurse */
  162.                       
  163.                       if ((strncmp(search_string,"?",1) == 0) && (lent == 0))
  164.                          return(FALSE);
  165.  
  166.                        else /* other ok character no we need to find this character in the
  167.                               target and recurse to try and resolve if the recursion fails
  168.                               we need to try further on in the string do this until we either
  169.                               suceed or the target string runs out and we fail */
  170.                          {
  171.  
  172.                            rvalue = FALSE;
  173.                            new_target = search_string;
  174.                           do
  175.                             {
  176.                               new_target = strchr(target,new_target[0]);
  177.                               if (new_target == NULL)
  178.                                 {
  179.                                  /* couldn't find character so we fail */
  180.                                  return(FALSE);
  181.                                 }
  182.                                else
  183.                                  {
  184.                                   /* found the char so recurse with it */
  185.                                   rvalue = wild_match(search_string,new_target);
  186.                                  }
  187.                                  /* increment here */
  188.                                  target = new_target;
  189.                                   if (strlen(target) == 1)
  190.                                     return(FALSE);
  191.                                   else ++target;
  192.                                } while (rvalue == FALSE);
  193.                                
  194.                               /* if we get out of the inner loop without returning it means
  195.                                we found a successfull match so we can return it */
  196.                                return(rvalue);
  197.                     } /* end of not * or ? */
  198.  
  199.                } /* end of srch string was * */
  200.  
  201.             else
  202.                {
  203.                  /* srch string is another character so find it in target */
  204.                  /* if next char in target is not the search string then we fail here
  205.                     otherwise we need to increment both and recurse */
  206.                  /* we also have a terminating case if if the length of the search string
  207.                     is greater than one and the length of the remaining target string is only
  208.                     one since the search string here is not a wildcard */
  209.                   if (lent <= 1) 
  210.                     return(FALSE);
  211.  
  212.                   else if (strncmp(search_string,target,1) != 0)
  213.                      return(FALSE);
  214.                   else 
  215.                      { /* they are equal so recurse through remainder of the string */
  216.                       ++search_string;
  217.                       ++target;
  218.                       return(wild_match(search_string,target));
  219.                      }
  220.                   } /* end search string is regular character case */
  221.  
  222.             } /* end len of srch string > 1 */           
  223.   } /* end of routine */
  224.  
  225.   /* -------------------------------------------------------------------------------------- */
  226.  
  227. void store_start_time()
  228.    {
  229.      start_time = GetTickCount();
  230.    }
  231.  
  232.  /* ----------------------------------------------------------------------------------------- */
  233.  void close_tape()
  234.    {
  235.       CloseHandle(hTape);
  236.       if (debug)
  237.         {
  238.           printf("TapeIO:--> Closed Tape Drive\n\n");
  239.           fflush(stdout);
  240.         }
  241.     tape_opened = FALSE;
  242.     }
  243.  
  244.  /* -------------------------------------------------------------------------------------------     */
  245.  
  246.  void terminate_program()
  247.  
  248.   {
  249.     if (tape_opened)
  250.          close_tape();
  251.     
  252.     exit(1);
  253. }
  254.  
  255.  /* ------------------------------------------------------------------------------------ */
  256.  
  257.     /* routine to report an error and terminat if severe */
  258.  
  259.  void report_error(text,rvalue,severity)
  260.    char text[20];
  261.    DWORD rvalue;
  262.    int severity;
  263.    {
  264.      printf("TapeIO: --> Error %s :",text);
  265.  
  266.      if (rvalue > 0) 
  267.         printf(" error code returned = %ld ",rvalue);
  268.         
  269.      printf(" Severity = %d \n",severity);
  270.      fflush(stdout);
  271.      
  272.      if (severity > 0)
  273.        terminate_program();
  274.       
  275.     }
  276.   /* --------------------------------------------------------------------------------------- */
  277.  
  278.  /*
  279.  * After a file/link/symlink/dir creation has failed, see if
  280.  * it's because some required directory was not present, and if
  281.  * so, create all required dirs.
  282.  */
  283. int
  284. make_dirs (pathname)
  285.      char *pathname;
  286. {
  287.   char *p;            /* Points into path */
  288.   int madeone = 0;        /* Did we do anything yet? */
  289.   int save_errno = errno;    /* Remember caller's errno */
  290.   int check;
  291.  
  292.  // if (errno != ENOENT)
  293.  //   return 0;            /* Not our problem */
  294.  
  295.   for (p = strchr (pathname, '\\'); p != NULL; p = strchr (p + 1, '\\'))
  296.     {
  297.       /* Avoid mkdir of empty string, if leading or double '/' */
  298.       if (p == pathname || p[-1] == '/')
  299.     continue;
  300.       /* Avoid mkdir where last part of path is '.' */
  301.       if (p[-1] == '.' && (p == pathname + 1 || p[-2] == '/'))
  302.     continue;
  303.       *p = 0;            /* Truncate the path there */
  304.       check = _mkdir (pathname);    /* Try to create it as a dir */
  305.             if (check == 0)
  306.     {
  307.       
  308.       if (debug)
  309.          {
  310.           printf("made directory %s \n",pathname);
  311.           fflush(stdout);
  312.          }
  313.  
  314.       madeone++;        /* Remember if we made one */
  315.       *p = '/';
  316.       continue;
  317.     }
  318.       *p = '/';
  319.       if (errno == EEXIST)    /* Directory already exists */
  320.     continue;
  321.       /*
  322.          * Some other error in the mkdir.  We return to the caller.
  323.          */
  324.       break;
  325.     }
  326.  
  327.   errno = save_errno;        /* Restore caller's errno */
  328.   return madeone;        /* Tell them to retry if we made one */
  329. }
  330.      
  331. /* --------------------------------------------------------------------------------------- */
  332.  
  333.   HANDLE open_file_for_write(filename)
  334.     char filename[360];
  335.      {
  336.  
  337.        HANDLE hFile;
  338.        int dirsmade;    
  339.  
  340.      hFile = CreateFile(filename,GENERIC_WRITE,FILE_SHARE_READ,(LPSECURITY_ATTRIBUTES)NULL,
  341.         CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,(HANDLE)NULL);
  342.  
  343.  
  344.   if (hFile == INVALID_HANDLE_VALUE)
  345.     {
  346.       
  347.       /* if we fail here assume it is because the path is not there and call our function to
  348.          create directories along the path */
  349.          /* if it fails after this we are out of luck */
  350.          
  351.         if (debug)
  352.           {
  353.             printf("create file %s failed calling make dirs to create any missing directories \n",
  354.                     filename);
  355.             fflush(stdout);
  356.            }
  357.          dirsmade = make_dirs(filename);
  358.          /* now try again to open it */
  359.           
  360.          hFile = CreateFile(filename,GENERIC_WRITE,FILE_SHARE_READ,(LPSECURITY_ATTRIBUTES)NULL,
  361.              CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,(HANDLE)NULL);
  362.          
  363.           if (hFile == INVALID_HANDLE_VALUE)
  364.             {
  365.              printf("TapeIO:--> Error Could not create file %s num dirs created = %d\n",
  366.                    filename,dirsmade);
  367.              report_error(" ",0,severe);   /* process error */
  368.             }
  369.  
  370.           else {
  371.                  if (debug)
  372.                    {
  373.                     printf("TapeIO:-->Created file %s \n",filename);
  374.                     fflush(stdout);
  375.                    }
  376.  
  377.                  return hFile;
  378.                 }
  379.        } /* end of first create failed */
  380.     else {
  381.           if (debug)
  382.             {
  383.              printf("TapeIO:-->Created file %s \n",filename);
  384.              fflush(stdout);
  385.              }
  386.  
  387.           return hFile;
  388.         }
  389.  }        
  390.  
  391. /* ------------------------------------------------------------------------------------ */
  392.   
  393.   int compress_file(filename,temppath)
  394.          char filename[];
  395.          char *temppath;
  396.     {
  397.        char TempFileName[30];
  398.        char cmd[200];
  399.        DWORD lcopied = 0;
  400.        int rvalue = 0;
  401.        char *nameloc;
  402.  
  403.       /* get the temp path */
  404.       /* append the temp filename and call gzip to execute and write the temp file name */
  405.       /* pass back the compressed file and we will back that up and remove it */
  406.     
  407.        lcopied = GetTempPath(100,temppath);
  408.        if (lcopied <= 0)
  409.           {
  410.            report_error(" Cannot retrieve temporary directory path \n",0,warning);
  411.               return(-1);
  412.           }
  413.        else
  414.          {
  415.            /* now create a temp name and concat path to name */
  416.              rvalue = GetTempFileName(temppath, /* dir. for temp. files            */
  417.                "NEW",                /* temp. filename prefix           */
  418.                  0,                    /* create unique name w/ sys. time */
  419.              (LPTSTR) TempFileName); /* buffer for name                 */
  420.  
  421.            if (rvalue == 0)
  422.              {
  423.                report_error(" Getting temp file name for compression \n",GetLastError(),warning);
  424.                  return(-1);
  425.              }
  426.            else 
  427.                {
  428.                 /* concat path to name */
  429.                 /* we only need to name form the temp file as it appends \temp\ to it
  430.                    and we already have the temppath */
  431.                   nameloc = strrchr(TempFileName,'\\');
  432.                   /* skip over slash */
  433.                   ++nameloc;
  434.  
  435.                   strcat(temppath,nameloc);
  436.                 
  437.                 if (debug)
  438.                   {
  439.                     printf("temp file for compress = %s \n",temppath);
  440.                     fflush(stdout);
  441.                   }
  442.    
  443.                    /* now create command to run zip having output go to tempfile. */
  444.     
  445.                strcpy(cmd, "gzip -c1n ");  /* use "gzip -c" for zwrite */
  446.                strncat(cmd, filename, sizeof(cmd)-strlen(cmd));
  447.                strcat(cmd," > ");
  448.                strcat(cmd, temppath);
  449.                if (debug)
  450.                  {
  451.                    printf("command for gzip = %s \n",cmd);
  452.                    fflush(stdout);
  453.                  }
  454.                         
  455.                 rvalue = system(cmd);
  456.                 if (rvalue != 0)
  457.                   {
  458.                    report_error("Spawn call to execute compression failed ",rvalue,severe);
  459.                    return(-1);
  460.                   }
  461.                 else return(0);
  462.  
  463.                } /* ok temp name */
  464.             } /* ok temp path name */
  465.       }
  466.  
  467.  
  468.  /* ------------------------------------------------------------------------------------ */
  469.   
  470.   int decompress_file(filename,temppath)
  471.          char filename[];
  472.          char temppath[];
  473.     {
  474.        char cmd[200];
  475.        int rvalue = 0;
  476.       HANDLE tHandle;
  477.  
  478.            /* open the file to make sure it can be created or force creation of paths */
  479.         tHandle = open_file_for_write(filename);
  480.         CloseHandle(tHandle);
  481.     
  482.         /* now create command to run zip having output go from tempfile to original file */
  483.     
  484.         
  485.                strcpy(cmd, "gzip -d < ");  /* use "gzip -c" for zwrite */
  486.                strncat(cmd, temppath, sizeof(cmd)-strlen(cmd));
  487.                strcat(cmd," > ");
  488.                strcat(cmd, filename);
  489.                if (debug)
  490.                  {
  491.                    printf("command for gzip = %s \n",cmd);
  492.                    fflush(stdout);
  493.                  }
  494.                 
  495.                 rvalue = system(cmd);
  496.                 if (rvalue != 0)
  497.                   {
  498.                    report_error("Spawn call to execute decompression failed ",rvalue,severe);
  499.                    return(-1);
  500.                   }
  501.                 else return(0);
  502.  
  503.    }
  504.    /* ------------------------------------------------------------------------------------ */
  505.      void write_setmarks(no_marks)
  506.       DWORD no_marks; 
  507.       {
  508.         DWORD rvalue;
  509.         
  510.         if (debug)
  511.           {
  512.             printf("Attempting to Write %d setmarks \n",no_marks);
  513.             fflush(stdout);
  514.           }
  515.        
  516.         rvalue = WriteTapemark(hTape, TAPE_SETMARKS,no_marks,FALSE);
  517.  
  518.           if (rvalue != NO_ERROR)
  519.             report_error(" Tape write setmarks error ",rvalue,severe);
  520.          
  521.       }
  522.     
  523.  
  524.   /* ------------------------------------------------------------------------------------ */
  525.      void write_filemarks(no_marks)
  526.       DWORD no_marks; 
  527.       {
  528.         DWORD rvalue;
  529.         
  530.         if (set_mark_positioning)
  531.           {
  532.            write_setmarks(no_marks);
  533.           }
  534.         else
  535.           {
  536.         if (debug)
  537.           {
  538.             printf("Attempting to Write %d filemarks \n",no_marks);
  539.             fflush(stdout);
  540.           }
  541.        
  542.         rvalue = WriteTapemark(hTape, TAPE_FILEMARKS,no_marks,FALSE);
  543.  
  544.           if (rvalue != NO_ERROR)
  545.             report_error(" Tape write filemarks error ",rvalue,severe);
  546.         } 
  547.       }
  548.     
  549.  
  550.  /* ------------------------------------------------------------------------------------ */
  551. void fill_in_todays_date()
  552.  
  553.   {         
  554.  
  555.    struct tm *newtime;
  556.    long ltime;
  557.  
  558.    time( <ime );
  559.  
  560.    /* Obtain Universal Coordinated Time: */
  561.    newtime = gmtime( <ime );
  562.    /* now fill in our date string */
  563.    sprintf(todays_date,"%2d-%2d-%2d",(newtime->tm_mon)+1,newtime->tm_mday,newtime->tm_year);
  564.     if (debug)
  565.       {
  566.         printf("filled in todays date with %s \n",todays_date);
  567.         fflush(stdout);
  568.       }
  569.  }
  570.  
  571.   /* ------------------------------------------------------------------------------------ */
  572.  
  573. /* routine to open up the tape drive  assumes only 1 tape drive  for now */
  574.  
  575.  void open_tape() 
  576.  
  577.   {
  578.       if (tape_opened == FALSE)
  579.         {
  580.  
  581.       hTape = CreateFile(
  582.            "\\\\.\\TAPE0",     /* name of tape device to open */
  583.            GENERIC_READ | GENERIC_WRITE, /* read-write access */
  584.            0,                    /* not used                  */
  585.            0,                    /* not used                  */
  586.            OPEN_EXISTING,        /* required for tape devices */
  587.            0,                    /* not used                  */
  588.            NULL);                /* not used                  */
  589.  
  590.  
  591.             if (hTape == NULL)
  592.                {
  593.                 report_error("Opening tape device \n",0,1);
  594.                }
  595.              else
  596.                   {
  597.                     if (debug) 
  598.                       { printf("TapeIO:--> Opened Tape drive.\n");
  599.                        fflush(stdout);
  600.                      }
  601.                     tape_opened = TRUE;
  602.                  }
  603.  
  604.             }
  605.     }
  606.  
  607.   /* ------------------------------------------------------------------------------------ */
  608.  
  609.  /* function to set block size */
  610.  
  611. void set_block_size(bsize)
  612.      unsigned long bsize;
  613.   {
  614.  
  615.     DWORD rvalue;
  616.      
  617.     tparms.BlockSize = bsize;
  618.     rvalue = SetTapeParameters(hTape,SET_TAPE_MEDIA_INFORMATION,&tparms);
  619.     if (rvalue != NO_ERROR)
  620.        {
  621.          report_error("Setting tape media information ",rvalue,severe);
  622.        }
  623.     else
  624.        {
  625.          if (debug)
  626.             {
  627.              printf("TapeIO:--> Set tape media parameters i.e. blocksize to %ld\n",bsize);
  628.              fflush(stdout);
  629.             }
  630.        }
  631.     }
  632.  
  633.  /* ------------------------------------------------------------------------------------ */
  634.  
  635.  /* rewind the tape always wait for resute no immediate mode */
  636.  
  637.  void rewind_tape()
  638.  {
  639.       DWORD rvalue;
  640.  
  641.        rvalue = PrepareTape(hTape,TAPE_LOAD,FALSE);
  642.     
  643.      if (rvalue != NO_ERROR)
  644.         {
  645.            report_error("rewinding tape ",rvalue,1);
  646.         }
  647.     else
  648.         {
  649.           current_tape_block_number = 0;
  650.           if (debug)
  651.             {
  652.               printf("TapeIO:-->Rewound Tape \n");
  653.               fflush(stdout);
  654.              }
  655.           }
  656.   }
  657.  
  658.   /* ------------------------------------------------------------------------------------ */
  659.  void space_setmarks(no_marks)
  660.      DWORD no_marks;
  661.    {
  662.      DWORD rvalue;
  663.      DWORD status;
  664.      DWORD rvalue2;
  665.      
  666.      if (debug)
  667.        {
  668.         printf("Attempting to Space %d setmarks\n",no_marks);
  669.         fflush(stdout);
  670.        }
  671.     
  672.           rvalue = SetTapePosition(hTape,TAPE_SPACE_SETMARKS,0,no_marks,0,FALSE); 
  673.           if (rvalue != NO_ERROR)
  674.              {
  675.                rvalue2 = GetLastError();
  676.                status = GetTapeStatus(hTape);
  677.                printf(" Tape positioning by setmarks error rvalue = %d tape status =%d getlsterror = %d\n",
  678.                         rvalue,status,rvalue2);
  679.                fflush(stdout);
  680.                report_error(" ",0,severe);
  681.              }
  682.         
  683.     } 
  684.  
  685. /* ------------------------------------------------------------------------------------ */
  686.  void space_filemarks(no_marks)
  687.      DWORD no_marks;
  688.    {
  689.      DWORD rvalue;
  690.      DWORD status;
  691.      DWORD rvalue2;
  692.      
  693.      if (set_mark_positioning)
  694.        {
  695.         space_setmarks(no_marks);
  696.        }
  697.      else {
  698.      if (debug)
  699.        {
  700.         printf("Attempting to Space %d file marks\n",no_marks);
  701.         fflush(stdout);
  702.        }
  703.     
  704.           rvalue = SetTapePosition(hTape,TAPE_SPACE_FILEMARKS,0,no_marks,0,FALSE); 
  705.           if (rvalue != NO_ERROR)
  706.              {
  707.                rvalue2 = GetLastError();
  708.                status = GetTapeStatus(hTape);
  709.                printf(" Tape positioning by filemarks error rvalue = %d tape status =%d getlsterror = %d\n",
  710.                         rvalue,status,rvalue2);
  711.                fflush(stdout);
  712.                report_error(" ",0,severe);
  713.              }
  714.         }
  715.     } 
  716.  
  717. /*   ---------------------------------------------------------------------------------- */
  718.  
  719.   void read_tape_block(buff)
  720.     char *buff;
  721.  
  722.     {
  723.       DWORD rdflag;
  724.  
  725.        rdflag = ReadFile(hTape, (LPSTR) buff, buffersize, &dwBytesRead, NULL);
  726.             
  727.             /* handle strange error case of not spacing over file mark correctly if on 
  728.             top of on assume it didn't space write and space over it */
  729.        if (rdflag != TRUE)
  730.          {
  731.           
  732.              if ((reading_header) && (GetLastError() == ERROR_NO_DATA_DETECTED))
  733.                { /* tape is not initialized */
  734.                  printf("ERROR while trying to read the tape header information, \n");
  735.                  printf("      no data was detected.  It appears that this tape     \n");
  736.                  printf("      has not been initialized (LKBACKUP -i) \n");
  737.                  fflush(stdout);
  738.                  report_error("Error reading tape",rdflag,severe);
  739.                }
  740.               else
  741.                 {
  742.                  printf("error reading block getlasterror %d getstatus %d \n",
  743.                          GetLastError(),GetTapeStatus(hTape));
  744.                  fflush(stdout);
  745.                  report_error("Error reading tape",0,severe);
  746.                 }
  747.              }    
  748.         else
  749.           {
  750.            ++current_tape_block_number;
  751.             if (debug)
  752.               {
  753.                 printf("TapeIO:--> Read %ld characters from the tape \n",dwBytesRead);
  754.                 fflush(stdout);
  755.               }
  756.            }
  757.     }
  758.  
  759.  /* ------------------------------------------------------------------------------------ */
  760.  
  761.     void write_tape_block(buff)
  762.     char buff[];
  763.  
  764.     {
  765.       DWORD wrflag;
  766.       DWORD dwBytesWritten;
  767.       
  768.         
  769.        wrflag = WriteFile(hTape, (LPSTR) buff, buffersize, &dwBytesWritten, NULL);
  770.        if (wrflag != TRUE)
  771.          {
  772.            report_error("Error writing tape",0,warning);
  773.          }
  774.         else
  775.           {
  776.             
  777.             if (debug)
  778.               {
  779.                 printf("TapeIO:--> Wrote %ld characters to the tape block = %d\n",
  780.                  dwBytesWritten,current_tape_block_number);
  781.                 fflush(stdout);
  782.               }
  783.             ++current_tape_block_number;
  784.            }
  785.     }
  786.             
  787.  /* ------------------------------------------------------------------------------------ */
  788.  
  789.   DWORD read_file_block(lhfile,buff)
  790.      
  791.      HANDLE lhfile;
  792.      char *buff;
  793.  
  794.     {
  795.       DWORD rdflag;
  796.       DWORD dwBytesRead = 0;
  797.  
  798.        rdflag = ReadFile(lhfile, (LPSTR) buff, buffersize, &dwBytesRead, NULL);
  799.        if (rdflag != TRUE)
  800.          {
  801.            report_error("Error reading file",0,warning);
  802.          }
  803.         else
  804.           {
  805.             if (debug)
  806.               {
  807.                 printf("TapeIO:--> Read %ld characters from file \n",dwBytesRead);
  808.                 fflush(stdout);
  809.               }
  810.            }
  811.  
  812.     return(dwBytesRead);
  813.     }
  814.  
  815.  /* ------------------------------------------------------------------------------------ */
  816.  
  817.     DWORD write_file_block(lhfile,buff,bytes)
  818.      
  819.      HANDLE lhfile;
  820.      char *buff;
  821.      int bytes;
  822.  
  823.     {
  824.       DWORD dwBytesWritten = 0;
  825.       DWORD wrflag;
  826.  
  827.        wrflag = WriteFile(lhfile, (LPSTR) buff, (DWORD)bytes, &dwBytesWritten, NULL);
  828.        if (wrflag != TRUE)
  829.          {
  830.            report_error("Error writing file",0,warning);
  831.          }
  832.         else
  833.           {
  834.             if (debug)
  835.               {
  836.                 printf("TapeIO:--> Wrote %ld characters to file \n",dwBytesWritten);
  837.                 fflush(stdout);
  838.               }
  839.            }
  840.      return(dwBytesWritten);
  841.     }
  842.             
  843.  /* ------------------------------------------------------------------------------------ */
  844.   
  845.  void dump_header_element(element_number)
  846.    int element_number;
  847.     {
  848.        
  849.        if (strncmp(raw_tape_header.header[element_number].saveset_name,"EMPTY001_____",13) != 0)
  850.          { /* ok not empty */
  851.            printf("%-8d",element_number);
  852.            printf("%16s",raw_tape_header.header[element_number].saveset_name);
  853.            printf("%13d   %15d   %8s\n",
  854.                raw_tape_header.header[element_number].original_size,
  855.                raw_tape_header.header[element_number].compressed_size,
  856.                raw_tape_header.header[element_number].date);
  857.            fflush(stdout);
  858.          }
  859.     }   
  860.    /* ------------------------------------------------------------------------------------ */
  861.  void space_end_of_data()
  862.    {
  863.      DWORD rvalue;
  864.      
  865.      if (debug)
  866.        {
  867.         printf("Attempting to Space to end of data \n");
  868.         fflush(stdout);
  869.        }
  870.     
  871.           rvalue = SetTapePosition(hTape,TAPE_SPACE_END_OF_DATA,0,0,0,FALSE); 
  872.           if (rvalue != NO_ERROR)
  873.              {
  874.                printf(" Tape positioning to end of data error rvalue = %d \n",rvalue);
  875.                fflush(stdout);
  876.                report_error(" ",rvalue,severe);
  877.              }
  878.     }
  879.  /* ----------------------------------------------------------------------------------------- */
  880.  
  881. void read_tape_index()
  882.  
  883.  {
  884.  
  885.   char block[bsize1];
  886.   int offset = 0;
  887.   int element_number = 0;
  888.   int end_element_number = 0;
  889.   int block_number = 0;
  890.  
  891.  
  892.   open_tape();
  893.   set_block_size(blocksize);
  894.   rewind_tape();
  895.   save_sets_on_tape = 0;
  896.   reading_header = TRUE;
  897.  
  898.   /* retrive the tape id first */
  899.   read_tape_block(block);
  900.   reading_header = FALSE;
  901.   if (strncmp(block,backup_version,12) != 0)
  902.         {
  903.        /* tape is foreign format */
  904.          printf("Warning tape is foreign in format.\n");
  905.          printf("You must initialize it first with LKBACKUP -i\n");
  906.          fflush(stdout);
  907.          rewind_tape();
  908.          close_tape(); 
  909.             exit(1);
  910.          } 
  911.  
  912.    /* now get to the tape index */
  913.    space_end_of_data();
  914.    /* now go back one filemark to get to the beginning of the index */
  915.    space_filemarks(-1);
  916.    space_filemarks(1);
  917.  
  918.  
  919.   /* retrieve all blocks of the tape index */
  920.  
  921.     current_tape_block_number = 0;
  922.     for (block_number = 0; block_number < blocks_per_header; block_number++)
  923.      {
  924.        read_tape_block(block);
  925.        offset = 0;
  926.        if (debug)
  927.          {
  928.            printf("Retrieved block %d %4096s \n",block_number,block);
  929.            fflush(stdout);
  930.          }
  931.  
  932.         element_number = (block_number * save_sets_per_block);
  933.         end_element_number = element_number + save_sets_per_block;
  934.  
  935.        do
  936.         { 
  937.          sscanf(block + offset,"%16s",raw_tape_header.header[element_number].saveset_name);
  938.          offset = offset + 16;
  939.           
  940.          sscanf(block + offset,"%10ud",&raw_tape_header.header[element_number].original_size);
  941.          offset=offset+10;
  942.                
  943.          sscanf(block + offset,"%10ud",&raw_tape_header.header[element_number].compressed_size);
  944.          offset=offset+12;
  945.          
  946.          sscanf(block + offset,"%8s",&raw_tape_header.header[element_number].date);
  947.          offset=offset+8;
  948.  
  949.          
  950.          if (debug)
  951.             {
  952.              printf("Current offset = %d \n",offset);
  953.              dump_header_element(element_number);
  954.              }
  955.         
  956.          ++element_number;
  957.          } while (element_number < end_element_number);
  958.        
  959.        } /* end for all blocks */
  960.    rewind_tape();
  961.   }
  962.   
  963.  /* ------------------------------------------------------------------------------------ */
  964. void write_tape_id()
  965.  
  966.   {
  967.   
  968.     char block[bsize1];
  969.     int offset;
  970.  
  971.     open_tape();
  972.     set_block_size(blocksize);
  973.     rewind_tape();
  974.     
  975.     /* fill in backup version and all blocks info */
  976.  
  977.         offset =  sprintf(block,"%12s %8s\0",backup_version,todays_date);
  978.         write_tape_block(block); /* force data to be written */
  979.         write_filemarks(1);
  980.   }
  981.     
  982. /* ------------------------------------------------------------------------------------- */
  983.       
  984. void write_tape_index()
  985.  
  986.  {
  987.  
  988.   char block[bsize1];
  989.   int element_number = 0;
  990.   int end_element_number = 0;
  991.   int offset = 0;
  992.   int block_number = 0;
  993.  
  994.   open_tape();
  995.   set_block_size(blocksize);
  996.   /* space to end of tape data */
  997.   rewind_tape();
  998.   space_end_of_data();
  999.   
  1000.     current_tape_block_number = 0;
  1001.    for (block_number = 0; block_number < blocks_per_header; block_number++)
  1002.      {
  1003.          offset = 0;
  1004.          for (element_number = (block_number * save_sets_per_block); element_number < (save_sets_per_block * (block_number + 1)); element_number++)
  1005.        {
  1006.          offset = offset + sprintf(block + offset,"%16s%10u %10u %8s",
  1007.                                       raw_tape_header.header[element_number].saveset_name,
  1008.                                       raw_tape_header.header[element_number].original_size,
  1009.                                       raw_tape_header.header[element_number].compressed_size,
  1010.                                       raw_tape_header.header[element_number].date);
  1011.         }
  1012.  
  1013.    if (debug)
  1014.      printf("TapeIO:--> character count in buffer = %d\n",offset);
  1015.      
  1016.     write_tape_block(block);
  1017.        
  1018.     if (debug)
  1019.       {
  1020.         printf("wrote block %d %4096s \n",block_number,block);
  1021.         fflush(stdout);
  1022.       }
  1023.  
  1024.      
  1025.     } /* end for loop */
  1026.       /* write the file mark after the index */
  1027.       rewind_tape();
  1028.   }
  1029.   
  1030.   
  1031.   /* ----------------------------------------------------------------------------- */
  1032.  
  1033.   void fill_in_header_element(name,osize,csize,date,element_number)
  1034.     char name[18];
  1035.     unsigned int osize;
  1036.     unsigned int csize;
  1037.     char date[9];
  1038.     int element_number;    
  1039.  
  1040.  
  1041.     {
  1042.            strncpy(raw_tape_header.header[element_number].saveset_name,name,16);
  1043.            raw_tape_header.header[element_number].original_size = osize;
  1044.            raw_tape_header.header[element_number].compressed_size = csize;
  1045.            strncpy(raw_tape_header.header[element_number].date,date,8);
  1046.            raw_tape_header.header[element_number].date[8] = '\0';
  1047.     
  1048.     } 
  1049.        
  1050.  
  1051.  /* ----------------------------------------------------------------------------------------- */
  1052.  
  1053.   void clear_header()
  1054.    {
  1055.  
  1056.     
  1057.     int element_number;
  1058.     
  1059.     for (element_number = 0; element_number< max_save_sets; element_number++)
  1060.        fill_in_header_element("EMPTY001______________________",0,0,"00-00-00",element_number);
  1061.    }   
  1062.  
  1063.  
  1064.  /* ------------------------------------------------------------------------------------------- */
  1065.   
  1066.   void initialize_tape()
  1067.    {
  1068.  
  1069.     if (debug)
  1070.        {
  1071.          printf("TapeIO:--> Initializing Tape.\n");
  1072.          fflush(stdout);
  1073.        }
  1074.     
  1075.     open_tape();
  1076.     rewind_tape();
  1077.     write_tape_id();
  1078.  
  1079.     clear_header();
  1080.     write_tape_index();
  1081.     
  1082.     }   
  1083.  
  1084.  
  1085.  /* ------------------------------------------------------------------------------------------- */
  1086.   
  1087.   void catalog_tape(no_good_backups)
  1088.     char *no_good_backups;
  1089.    {
  1090.     int no_backups;
  1091.     int rvalue;
  1092.     int element_number;
  1093.     char current_name[40];
  1094.     char current_number[4];
  1095.  
  1096.     rvalue = sscanf(no_good_backups,"%d",&no_backups);
  1097.     if (rvalue == 0)
  1098.       {
  1099.         printf("Error %s is not a legal integer value for the number of good backups on the tape \n",
  1100.                              no_good_backups);
  1101.         fflush(stdout);
  1102.         report_error(" ",0,severe);
  1103.       }
  1104.      /* check for legal number */
  1105.       if ((no_backups < 1) || (no_backups > max_save_sets))   
  1106.            {
  1107.             printf("Error %d is not a legal integer value for the number of good backups on the tape \n",
  1108.                              no_backups);
  1109.         fflush(stdout);
  1110.         report_error(" ",0,severe);
  1111.         }
  1112.       /* otherwise do it */
  1113.       /* fill in the correct number in the header */
  1114.       clear_header();
  1115.       for (element_number = 0; element_number < no_backups; element_number++)
  1116.          {
  1117.           strcpy(current_name,"saveset");
  1118.           sprintf(current_number,"%d",element_number+1);
  1119.           strcat(current_name,current_number);
  1120.           while (strlen(current_name) < 30)
  1121.             strcat(current_name," ");
  1122.           if (debug)
  1123.             {
  1124.               printf("Filling in header with %s for saveset name len = %d \n",current_name, 
  1125.                        strlen(current_name));
  1126.               fflush(stdout);
  1127.             } 
  1128.            fill_in_header_element(current_name,0,0,"00-00-00",element_number);
  1129.           }    /* end of loop */
  1130.          /* now space to end of data and write out the index file again */
  1131.  
  1132.           if (!quiet_mode)
  1133.           {
  1134.            printf("Attempting to restore the tape index\n");
  1135.            fflush(stdout);
  1136.           }
  1137.  
  1138.     if (debug)
  1139.        {
  1140.          printf("TapeIO:--> Initializing Tape.\n");
  1141.          fflush(stdout);
  1142.        }
  1143.     
  1144.     write_tape_index();
  1145.     
  1146.     }   
  1147.  
  1148.  
  1149. /* ------------------------------------------------------------------------------------ */
  1150.  void erase_tape(secure)
  1151.     BOOLEAN secure;
  1152.  
  1153.     {        
  1154.  
  1155.      DWORD rvalue;
  1156.       
  1157.       open_tape();
  1158.       rewind_tape();
  1159.  
  1160.       current_tape_block_number = 0;
  1161.       if (secure)
  1162.           rvalue = EraseTape(hTape,TAPE_ERASE_LONG,FALSE);
  1163.       else rvalue = EraseTape(hTape,TAPE_ERASE_SHORT,FALSE);
  1164.  
  1165.      if (rvalue != NO_ERROR)
  1166.         {
  1167.            report_error("Erasing tape ",rvalue,severe);
  1168.         }
  1169.  
  1170.     else
  1171.         
  1172.         {
  1173.           if (debug)
  1174.             {
  1175.               printf("TapeIO:-->Erased Tape");
  1176.               if (secure) 
  1177.                 printf(" Secure/long \n");
  1178.               else printf(" Short \n");
  1179.               fflush(stdout);
  1180.              }
  1181.           }
  1182.   }
  1183.  
  1184. /* ------------------------------------------------------------------------------------ */
  1185.  
  1186.   HANDLE open_file_for_read(filename,doing_backup_restore)
  1187.     char filename[200];
  1188.     BOOLEAN doing_backup_restore;
  1189.      {
  1190.  
  1191.        HANDLE hFile;    
  1192.  
  1193.      hFile = CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,(LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN | FILE_FLAG_BACKUP_SEMANTICS,(HANDLE)NULL);
  1194.  
  1195.  
  1196.   if (hFile == INVALID_HANDLE_VALUE)
  1197.     {
  1198.       printf("TapeIO:--> Error Could not open file %s\n",filename);
  1199.       fflush(stdout);
  1200.       if (doing_backup_restore)
  1201.         {
  1202.          return(NULL);
  1203.         }
  1204.        else
  1205.         report_error(" ",0,severe);   /* process error */
  1206.     }
  1207.  
  1208.    else {
  1209.           if (debug)
  1210.             {
  1211.              printf("TapeIO:-->Opened file %s \n",filename);
  1212.              fflush(stdout);
  1213.              }
  1214.  
  1215.     return hFile;
  1216.         }
  1217.  }        
  1218.  
  1219.  
  1220. /* --------------------------------------------------------------------------------------- */
  1221. void open_index_file()
  1222.   {
  1223.  
  1224.  DWORD lcopied = 0;
  1225.  int rvalue = 0;
  1226.  char TempName[40];
  1227.  char *nameloc;
  1228.  
  1229.  if (!tempindexfileopen)
  1230.    {
  1231.        lcopied = GetTempPath(100,TempIndexFileName);
  1232.        if (lcopied <= 0)
  1233.            report_error(" Cannot retrieve temporary directory path \n",0,severe);
  1234.        else
  1235.          {
  1236.            /* now create a temp name and concat path to name */
  1237.              rvalue = GetTempFileName(TempIndexFileName, /* dir. for temp. files            */
  1238.                "NEW",                /* temp. filename prefix           */
  1239.                  0,                    /* create unique name w/ sys. time */
  1240.              (LPTSTR) TempName); /* buffer for name                 */
  1241.  
  1242.            if (rvalue == 0)
  1243.                report_error(" Getting temp file name for index file \n",rvalue,severe);
  1244.            else 
  1245.                {
  1246.                 /* concat path to name */
  1247.                 /* we only need to name form the temp file as it appends \temp\ to it
  1248.                    and we already have the temppath */
  1249.                   nameloc = strrchr(TempName,'\\');
  1250.                   /* skip over slash */
  1251.                   ++nameloc;
  1252.  
  1253.                   strcat(TempIndexFileName,nameloc);
  1254.                    
  1255.              TempIndexFile = fopen(TempIndexFileName,"w");
  1256.            
  1257.                 if (TempIndexFile == NULL)
  1258.                   report_error(" Could not create temporary index file.",0,severe);
  1259.                 else 
  1260.                   {
  1261.                    if (debug)
  1262.                      {
  1263.                        printf("created temp index file %s \n",TempIndexFileName);
  1264.                        fflush(stdout);
  1265.                       }
  1266.  
  1267.                      tempindexfileopen = TRUE;
  1268.                   }
  1269.                } /* name ok */
  1270.          } /* path ok */
  1271.       } /* not already open */
  1272.    }
  1273.           
  1274. /* --------------------------------------------------------------------------------------- */
  1275. void write_index_entry(fpath,size,compressed_size,date,start_block)
  1276.    
  1277.    char fpath[512];
  1278.    unsigned int size;
  1279.    unsigned int compressed_size;
  1280.    char date[9];
  1281.    int start_block;
  1282.  
  1283.    {
  1284.      int compression_percent = (int)(100.00 * (1.000 - (float)((float)compressed_size/(float)size)));
  1285.  
  1286.          if ((compressed_size == 0) && (size > 0))
  1287.            compression_percent = 0;
  1288.  
  1289.          if (!tempindexfileopen)
  1290.            open_index_file();
  1291.  
  1292.         /* write info to command line and then to index file */
  1293.         if (!quiet_mode)
  1294.           {
  1295.            printf("%s %d %d (%d)%%  %8s %d\n",
  1296.                                     fpath,size,compressed_size,compression_percent,date,start_block);
  1297.           }
  1298.  
  1299.         fprintf(TempIndexFile,"%350s %10d %10d %8s %10d \n",
  1300.            fpath,size,compressed_size,date,start_block);
  1301.         
  1302.          fflush(stdout);
  1303.         fflush(TempIndexFile);
  1304.   }
  1305.  
  1306. /* --------------------------------------------------------------------------------------*/
  1307.  void close_index_file()
  1308.   {  
  1309.  
  1310.      if (tempindexfileopen)
  1311.        {
  1312.          fclose(TempIndexFile);
  1313.          if (debug)
  1314.            {
  1315.              printf("closed temp index file \n");
  1316.              fflush(stdout);
  1317.            }
  1318.         tempindexfileopen = FALSE;
  1319.         }
  1320.   }
  1321.  
  1322. /* --------------------------------------------------------------------------------------*/
  1323.  
  1324.  void backup_file(filename,size,attributes,indexfile)
  1325.     char filename[];
  1326.     DWORD size;
  1327.     DWORD attributes;
  1328.     BOOLEAN indexfile;
  1329.  
  1330.     {
  1331.       char lbuffer[bsize1];
  1332.       HANDLE hFile;
  1333.       LPTSTR ppszFilePart;    /* address of filename in path    */
  1334.       DWORD rvalue;
  1335.       FILETIME wtime;
  1336.       BOOLEAN oktime;
  1337.       DWORD dwAttrs;
  1338.       unsigned int compress_size = 0;
  1339.       char savepath[513];
  1340.       int start_block = current_tape_block_number;
  1341.       int ldwBytesRead = 0;
  1342.       char compress_name[300];
  1343.       BOOLEAN backing_up_compressed_file = FALSE;
  1344.       unsigned short dostime;
  1345.       unsigned short dosdate;
  1346.       char file_date[9];
  1347.  
  1348.       
  1349.      hFile = open_file_for_read(filename,TRUE);
  1350.      
  1351.   /* now since file opened ok write out first buffer with file name and
  1352.                size */
  1353.      if (hFile == NULL) /* skip it since it couldn't be opened */
  1354.        {
  1355.          ++filesskipped;
  1356.          printf("Warning skipping file %s which could not be opened. \n",filename);
  1357.          fflush(stdout);
  1358.        }
  1359.  
  1360.      else
  1361.  
  1362.       {
  1363.  
  1364.     if (debug)
  1365.       {
  1366.         printf("TapeIO:--> Backing up %s size is %ld \n",filename,size);
  1367.         fflush(stdout);
  1368.       }         
  1369.        /* write full path to file */
  1370.        /* but erase first 3 chars ie i:\ so that restore is not drive specific */
  1371.  
  1372.     rvalue = GetFullPathName((LPCSTR)filename,buffersize,(LPTSTR)lbuffer,&ppszFilePart);
  1373.     if (rvalue == 0)
  1374.        {
  1375.          CloseHandle(hFile);
  1376.          report_error(" Path + file name longer than max buffer size (512)\n",rvalue,severe);
  1377.        }
  1378.     
  1379.     lbuffer[0] = ' ';
  1380.        lbuffer[1] = ' ';
  1381.     lbuffer[2] = ' ';
  1382.       
  1383.     strcpy(savepath,lbuffer);
  1384.  
  1385.     if (debug)
  1386.       {
  1387.         printf("TapeIO:--> Backing up %s size is %ld \n",lbuffer,size);
  1388.         fflush(stdout);
  1389.       }         
  1390.     
  1391.           /* (*ctime).dwLowDateTime = 0;
  1392.           (*atime).dwLowDateTime = 0;
  1393.          (*wtime).dwLowDateTime = 0;
  1394.          (*ctime).dwHighDateTime = 0;
  1395.          (*atime).dwHighDateTime = 0;
  1396.          (*wtime).dwHighDateTime = 0;
  1397.             */
  1398.     /* now write file date/time and attributes */
  1399.      oktime = GetFileTime(hFile,NULL,NULL,&wtime);
  1400.      if (!oktime)
  1401.        report_error(" Couldn't retrieve file time \n",0,warning);
  1402.      
  1403.      /* get attr string */
  1404.    
  1405.     /* get the files attributes and return true if it is a directory */
  1406.       
  1407.       dwAttrs = GetFileAttributes(filename);
  1408.     /* the the other dates when tried to accessed report an access violation if not on NTFS */
  1409.  
  1410.     sprintf(lbuffer + rvalue," %d %d %d %d",size,attributes, 
  1411.                wtime.dwLowDateTime,wtime.dwHighDateTime);
  1412.   
  1413.        write_tape_block(lbuffer);
  1414.        
  1415.     if (debug)
  1416.       {
  1417.         printf("wrote block for file header %2048s \n",lbuffer);
  1418.         fflush(stdout);
  1419.       }
  1420.  
  1421.   if ((compress) && (!indexfile)) /* do not compress the index file */
  1422.     {
  1423.       /* close the file handle */
  1424.       CloseHandle(hFile);
  1425.       compress_size = 0;
  1426.       rvalue = compress_file(filename,&compress_name);
  1427.       if (rvalue == 0)
  1428.         {
  1429.           /* file was compressed ok so close the file stream reopen with compressed file */
  1430.           CloseHandle(hFile);
  1431.           hFile = open_file_for_read(compress_name,FALSE);
  1432.           backing_up_compressed_file = TRUE;
  1433.          }
  1434.        }
  1435.  
  1436.      /* if compression failed then default to uncompressed file */
  1437.      /* backup the file pointed to by hfile either compressed if worked or uncompressed otherwise */
  1438.  
  1439.     do {
  1440.           dwBytesRead = read_file_block(hFile,lbuffer);
  1441.           if (dwBytesRead > 0)
  1442.             {  
  1443.               if (debug)
  1444.                 {
  1445.                   printf("TapeIO:-->Attempting to write the buff to tape\n");
  1446.                   fflush(stdout);
  1447.                  }
  1448.                        
  1449.               write_tape_block(lbuffer);
  1450.               if (backing_up_compressed_file)
  1451.                  compress_size = compress_size + dwBytesRead;
  1452.                     
  1453.              } /* bytes > 0 */
  1454.   
  1455.         } while (dwBytesRead == buffersize);
  1456.       /* close the file handle */
  1457.       CloseHandle(hFile);
  1458.       /* delete temp file if we have compression */
  1459.       if (backing_up_compressed_file)
  1460.          {
  1461.          DeleteFile(compress_name);
  1462.          ++filescompressed;
  1463.          total_backup_size = total_backup_size + compress_size;
  1464.          }
  1465.        else {
  1466.               ++filesuncompressed;
  1467.               total_backup_size = total_backup_size + size;
  1468.             }
  1469.       
  1470.       /* for now use todays date later fill in with file date */
  1471.  
  1472.       if (!indexfile)
  1473.         {
  1474.           /* need to convert to file date for the index entry */
  1475.           if (!FileTimeToDosDateTime(&wtime,&dosdate,&dostime))
  1476.             {
  1477.              
  1478.              printf("Warning couldn't convert file date to dos date using todays date \n");
  1479.              fflush(stdout);
  1480.              write_index_entry(savepath,size,compress_size,todays_date,start_block);
  1481.             }
  1482.            /* ok we got the date no set it into varibles and convert to string */
  1483.            else
  1484.            { 
  1485.             int nDay = dosdate & 0x1f;
  1486.             int nMonth = (dosdate >> 5) & 0x0f;
  1487.             int nYear = (dosdate >> 9) + 80;
  1488.  
  1489.             /* now convert to string */
  1490.             sprintf(file_date,"%02d-%02d-%02d",nMonth,nDay,nYear);
  1491.             write_index_entry(savepath,size,compress_size,file_date,start_block);
  1492.            }
  1493.  
  1494.         }
  1495.  
  1496.       total_original_size = total_original_size + size;
  1497.       } /* skip file */
  1498.     }
  1499.  /* ------------------------------------------------------------------------------------- */
  1500.  void set_file_info(filename,lowtime,hightime,lattrs)
  1501.     
  1502.     char *filename;
  1503.     DWORD lowtime;
  1504.     DWORD hightime;
  1505.     DWORD lattrs;
  1506.  
  1507.    {
  1508.  
  1509.        FILETIME local_file_time;
  1510.       HANDLE readHandle;
  1511.  
  1512.          /* attempt to set date and file attributes */
  1513.                if (lattrs != 0)
  1514.                  {
  1515.                   if (debug)
  1516.                     {
  1517.                      printf("Attempting to set file attributes to %d \n",lattrs);
  1518.                      fflush(stdout);
  1519.                     }
  1520.  
  1521.                if (!SetFileAttributes(filename,lattrs))
  1522.                   {
  1523.                    printf("Warning couldn't set file %s attributes to %s during restore \n",filename,lattrs);
  1524.                    fflush(stdout);
  1525.                   }
  1526.                  }
  1527.                 
  1528.                /* now fill in a wtime struct and attempt to reset the time */
  1529.                 local_file_time.dwLowDateTime = lowtime;
  1530.                 local_file_time.dwHighDateTime = hightime;
  1531.  
  1532.                 readHandle = open_file_for_read(filename,TRUE);
  1533.  
  1534.                 if (!SetFileTime(readHandle,NULL,NULL,&local_file_time))
  1535.                   {
  1536.                    printf("Warning couldn't set file last write time for %s error = %d \n",
  1537.                            filename,GetLastError());
  1538.                    fflush(stdout);
  1539.                   }
  1540.                 CloseHandle(readHandle);
  1541.       }
  1542.  
  1543.   /* --------------------------------------------------------------------------------------*/
  1544.  
  1545.  void restore_file(filename,size,csize,isindexfile)
  1546.     char *filename;
  1547.     int size;
  1548.     int csize;
  1549.     BOOLEAN isindexfile;
  1550.  
  1551.           /* return the restored file name */
  1552.           /* if it is the index file create it as orinally but append the temp drive to it */
  1553.  
  1554.     {
  1555.       char temppath[400];
  1556.       BOOLEAN compressed = FALSE;
  1557.       char lbuffer[bsize1];
  1558.       HANDLE hFile;
  1559.       int restore_size;
  1560.       DWORD lcopied = 0;
  1561.       unsigned int rvalue2;
  1562.       char TempFileName[400];
  1563.       char *nameloc;
  1564.       BOOLEAN done;
  1565.       int size_restored_so_far = 0;
  1566.       char lname[400];
  1567.       DWORD lowtime;
  1568.       DWORD hightime;
  1569.       DWORD lattrs;
  1570.       int lsize;
  1571.       
  1572.           
  1573.       if (csize > 0)
  1574.        {
  1575.         compressed = TRUE;
  1576.         restore_size = csize;
  1577.               /* create a temp file name/path to restore the file to */
  1578.      
  1579.        }  /* compress */
  1580.      else
  1581.         {
  1582.          compressed = FALSE;
  1583.          restore_size = size;
  1584.         }
  1585.  
  1586.     
  1587.       /* get the temp path for a compress restore or restore of the index file */
  1588.      
  1589.      if (compressed || isindexfile)
  1590.        {
  1591.        lcopied = GetTempPath(100,temppath);
  1592.        if (lcopied <= 0)
  1593.           {
  1594.            report_error(" Cannot retrieve temporary directory path \n",0,severe);
  1595.           }
  1596.        else
  1597.          {
  1598.            /* now create a temp name and concat path to name */
  1599.              rvalue2 = GetTempFileName(temppath, /* dir. for temp. files            */
  1600.                "NEW",                /* temp. filename prefix           */
  1601.                  0,                    /* create unique name w/ sys. time */
  1602.              (LPTSTR) TempFileName); /* buffer for name                 */
  1603.  
  1604.            if (rvalue2 == 0)
  1605.              {
  1606.                report_error(" Getting temp file name for decompression \n",rvalue2,severe);
  1607.              }
  1608.            else 
  1609.                {
  1610.                 /* concat path to name */
  1611.                 /* we only need to name form the temp file as it appends \temp\ to it
  1612.                    and we already have the temppath */
  1613.                   nameloc = strrchr(TempFileName,'\\');
  1614.                   /* skip over slash */
  1615.                   ++nameloc;
  1616.  
  1617.                   strcat(temppath,nameloc);
  1618.                 
  1619.                 if (debug)
  1620.                   {
  1621.                     printf("temp file for decompress = %s \n",temppath);
  1622.                     fflush(stdout);
  1623.                   }
  1624.    
  1625.                    } /* name ok */
  1626.              } /* path ok */ 
  1627.           } /* for index or compressed file */
  1628.       else
  1629.         {
  1630.           /* not compressed or index means regular file uncompressed to copy file name to temppath */
  1631.           strcpy(temppath,filename);
  1632.         }
  1633.  
  1634.      if (debug)
  1635.        {
  1636.         printf("TapeIO:--> Restoring file about to read name block \n");
  1637.         fflush(stdout);
  1638.        }         
  1639.       read_tape_block(lbuffer);
  1640.       if (debug)
  1641.          {
  1642.           printf("In restore file got name block number %s\n",lbuffer);
  1643.           fflush(stdout);
  1644.          }
  1645.  
  1646.     
  1647.     if (isindexfile)
  1648.       {
  1649.        /* get the size out of the buffer */
  1650.         sscanf(lbuffer,"%350s %10d",filename,&size);
  1651.         restore_size = size; /* index file is not compressed */
  1652.         if (debug)
  1653.           {
  1654.             printf("got name/size for index file name = %s size = %d \n",filename,size);
  1655.             fflush(stdout);
  1656.           }
  1657.       }
  1658.  
  1659.    /* check to make sure name in file matches restore name ignore check for index file */
  1660.    else
  1661.       {     /* must skip over first 3 characters in buffer that had e:\ etc. in it */
  1662.        
  1663.          /* get times and attributes */
  1664.         lsize = 0;
  1665.         lattrs = 0;
  1666.         lowtime = 0;
  1667.         hightime = 0;
  1668.         sscanf(lbuffer + 3,"%s %d %d %d %d",lname,&lsize,&lattrs,&lowtime,&hightime);
  1669.  
  1670.        if (strncmp(filename,lname,strlen(filename)) != 0)
  1671.          {
  1672.           printf("Tape IO Error Name in buffer %350s does not match restore file name %s \n",
  1673.                      lbuffer,filename);
  1674.           fflush(stdout);
  1675.           report_error(" ",0,severe);
  1676.          }
  1677.       }
  1678.   /* now restore the file wherever (to temppath)  */
  1679.      
  1680.       if (debug)
  1681.         {
  1682.           printf("attempting to open for write/restore: %s \n",temppath);
  1683.           fflush(stdout);
  1684.         }
  1685.  
  1686.       hFile = open_file_for_write(temppath);
  1687.       done = FALSE;
  1688.       size_restored_so_far = 0;
  1689.  
  1690.      /* restore the file pointed to by hfile */
  1691.     do {
  1692.           read_tape_block(lbuffer);
  1693.               
  1694.               if (debug)
  1695.                 {
  1696.                   printf("TapeIO:-->Attempting to write the buff to file\n");
  1697.                   fflush(stdout);
  1698.                  }
  1699.                        
  1700.               if ((size_restored_so_far + blocksize) <= restore_size)
  1701.                 {
  1702.                   write_file_block(hFile,lbuffer,blocksize);
  1703.                   if ((size_restored_so_far + blocksize) == restore_size)
  1704.                     done = TRUE;
  1705.  
  1706.                   size_restored_so_far = size_restored_so_far + blocksize;
  1707.                  }
  1708.  
  1709.                else /* write last partial block */
  1710.                  {
  1711.                    write_file_block(hFile,lbuffer,restore_size - size_restored_so_far);
  1712.                    if (debug)
  1713.                      {
  1714.                        printf("Writing out last partial block of %d characters \n",
  1715.                                restore_size - size_restored_so_far);
  1716.                        fflush(stdout);
  1717.                       }
  1718.                      done = TRUE;
  1719.                      size_restored_so_far = size_restored_so_far + (restore_size - size_restored_so_far);
  1720.                      
  1721.                  } /* bytes > 0 */
  1722.  
  1723.         } while (!done);
  1724.  
  1725.       /* close the file handle */
  1726.       CloseHandle(hFile);
  1727.     
  1728.     /* now if the file was uncompressed or the index file 
  1729.         we are done otherwise if it was compressed we need to decompress it to the
  1730.         original file */
  1731.  
  1732.        if (!compressed)
  1733.          {
  1734.               if (!isindexfile)
  1735.                  set_file_info(filename,lowtime,hightime,lattrs);
  1736.                    
  1737.               if ((!quiet_mode) && (!isindexfile))
  1738.                 {
  1739.                   printf("Restored Uncompressed file %s \n",filename);
  1740.                   fflush(stdout);
  1741.                 }
  1742.  
  1743.  
  1744.               ++filesuncompressed;
  1745.                 total_bytes_read = total_bytes_read + size;
  1746.               /* if index file copy name to ifle name */
  1747.               if (isindexfile)
  1748.                 strcpy(filename,temppath);
  1749.          }
  1750.  
  1751.         else /* compressed */
  1752.           {
  1753.                /* now decompress it */
  1754.                /* when decompressing the name somehow gets changes to / probably in the mkdir
  1755.                   routine can't find it so copy it, and copy back after decompress */
  1756.             strcpy(lname,filename);
  1757.  
  1758.             if (decompress_file(filename,temppath) == -1)
  1759.               {
  1760.                 printf("Warning couldn't decompress file %s \n",lname);
  1761.                 printf("File NOT RESTORED \n");
  1762.                 printf("Leaving temp file %s for an attempt at manual decompression. \n",temppath);
  1763.                 fflush(stdout);
  1764.                }
  1765.              else
  1766.                {
  1767.                  /* copy back */
  1768.                  strcpy(filename,lname);
  1769.  
  1770.                  set_file_info(filename,lowtime,hightime,lattrs);
  1771.                  if (!quiet_mode)
  1772.                    {
  1773.                      printf("Restored Compressed file %s\n",filename);
  1774.                      fflush(stdout);
  1775.                    } 
  1776.                  ++filescompressed;
  1777.                  total_bytes_read = total_bytes_read + csize;
  1778.                  DeleteFile(temppath);
  1779.                 }
  1780.                 
  1781.           }
  1782.  
  1783.     }
  1784.  
  1785.    /* -------------------------------------------------------------------------------------- */
  1786.    void print_backup_stats()
  1787.      {
  1788.        DWORD total_time;
  1789.        unsigned long int minutes;
  1790.        unsigned long int backuprate;
  1791.  
  1792.        total_time = GetTickCount() - start_time;
  1793.  
  1794.         if (debug)
  1795.           printf("total time = %d \n",total_time);
  1796.  
  1797.        minutes = (int)(((float)total_time) / ((float)60000.0000));
  1798.                                           /* 60 * 1000 which equal the number 
  1799.                                             of milliseconds in a minute */
  1800.  
  1801.        backuprate = (int)(((float)total_original_size / (float)minutes) / (float)1024.000);
  1802.        if (!quiet_mode)
  1803.          {
  1804.        printf("\n----------------------------------------------------------------\n");
  1805.        printf("Files Backed Up          --> %d \n",filescompressed+filesuncompressed);
  1806.        printf("  No Compressed          --> %d \n",filescompressed);
  1807.        printf("  No UnCompressed        --> %d \n",filesuncompressed);
  1808.        printf("Files Skipped            --> %d \n\n",filesskipped);
  1809.        printf("Original Size            --> %d \n",total_original_size);
  1810.        printf("Size on Tape             --> %d \n",total_backup_size); 
  1811.        printf("Compression Ratio        --> (%2d%%)\n\n",
  1812.           (int)(100.00 * (1.000 - (float)((float)total_backup_size/(float)total_original_size))));
  1813.        printf("Backup Time (minutes)    --> %d \n",minutes);
  1814.        printf("Backup Rate K/minute     -->    %d \n",backuprate);
  1815.        printf("\n----------------------------------------------------------------\n");
  1816.        fflush(stdout);
  1817.        }
  1818.      }
  1819.  
  1820.   /* --------------------------------------------------------------------------------------- */
  1821.    void print_restore_stats()
  1822.      {
  1823.        DWORD total_time;
  1824.        unsigned long int minutes;
  1825.        unsigned long int restoreuprate;
  1826.  
  1827.        total_time = GetTickCount() - start_time;
  1828.  
  1829.         if (debug)
  1830.           printf("total time = %d \n",total_time);
  1831.  
  1832.        minutes = (int)(((float)total_time) / ((float)60000.0000));
  1833.                                           /* 60 * 1000 which equal the number 
  1834.                                             of milliseconds in a minute */
  1835.  
  1836.        restoreuprate = (int)(((float)total_bytes_read / (float)minutes) / (float)1024.000);
  1837.        if (!quiet_mode)
  1838.          {
  1839.        printf("\n----------------------------------------------------------------\n");
  1840.        printf("Files Restored           --> %d \n",filescompressed+filesuncompressed);
  1841.        printf("  No Compressed          --> %d \n",filescompressed);
  1842.        printf("  No UnCompressed        --> %d \n\n",filesuncompressed);
  1843.        printf("Total Bytes Restored     --> %d \n",total_bytes_read); 
  1844.        printf("Restore Time (minutes)   --> %d \n",minutes);
  1845.        printf("Restore Rate K/minute    -->    %d \n",restoreuprate);
  1846.        printf("\n----------------------------------------------------------------\n");
  1847.        fflush(stdout);
  1848.        }
  1849.      }
  1850.   /* --------------------------------------------------------------------------------------- */
  1851.    void write_directory_file()
  1852.   {
  1853.    int fsize;
  1854.  
  1855.    fsize = ftell(TempIndexFile);
  1856.    close_index_file();
  1857.    /* size is not important for this file */
  1858.    /*ignore drive letter on file */
  1859.    /* we need the file size here otherwise it will not know how much to restore */
  1860.  
  1861.    backup_file(TempIndexFileName,fsize,0,TRUE); /* indicate this is the directory file */
  1862.    /* now delete the file */
  1863.    DeleteFile(TempIndexFileName);
  1864.   
  1865.   }
  1866.  
  1867. /* ------------------------------------------------------------------------------------------ */
  1868.               
  1869. void process_write_directory(SearchString,process_subdirs)
  1870.   char *SearchString;
  1871.   BOOLEAN process_subdirs;
  1872.  
  1873. /* if process subdirs set first go through entire directory then do it again and process subdirs */
  1874.  
  1875.  {
  1876.      WIN32_FIND_DATA FileData;
  1877.      HANDLE hSearch;
  1878.      BOOLEAN done = FALSE;
  1879.      BOOLEAN rvalue;
  1880.      BOOLEAN onefound = FALSE;
  1881.  
  1882. /* Start searching for  files in the current directory. */
  1883.  
  1884.    hSearch = FindFirstFile(SearchString, &FileData);
  1885.    if (hSearch == INVALID_HANDLE_VALUE)
  1886.      {
  1887.        /* no files in search parameters */
  1888.        if ((debug) && (!process_subdirs))
  1889.          {
  1890.            printf("TapeIO:--> Warning no files found in search parameter %s \n",SearchString);   
  1891.            fflush(stdout);
  1892.          }
  1893.         if (!process_subdirs)
  1894.           {
  1895.             FindClose(hSearch);
  1896.               return; /* cannot do this directory */
  1897.           }
  1898.      }
  1899.     else onefound = TRUE;
  1900.     
  1901.     if (onefound) 
  1902.       {  
  1903.    /* get size and process file only gets here if handle opened ok */
  1904.    /* skip directories */
  1905.      if (!(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
  1906.       backup_file(FileData.cFileName,FileData.nFileSizeLow,FileData.dwFileAttributes,FileData,FALSE);
  1907.   
  1908.      /* loop through remainder of files */
  1909.  
  1910.     do {
  1911.        rvalue = FindNextFile(hSearch,&FileData);
  1912.        if ((rvalue == FALSE) && (GetLastError() == ERROR_NO_MORE_FILES))
  1913.          {
  1914.             done = TRUE;
  1915.             FindClose(hSearch);
  1916.          }  
  1917.  
  1918.        else
  1919.           {
  1920.           if (!(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
  1921.            backup_file(FileData.cFileName,FileData.nFileSizeLow,FileData.dwFileAttributes,FALSE);
  1922.             }
  1923.         } while (!done);
  1924.  
  1925.      } /* onefound */
  1926.  
  1927.  /* now if process subdirs set go through and stop at all subdirs and recurse */
  1928.   if (process_subdirs)
  1929.     {
  1930.       done = FALSE;
  1931.  
  1932.       /* Start searching for all subdirectories to recurse on */
  1933.       hSearch = FindFirstFile("*", &FileData);
  1934.       if (hSearch == INVALID_HANDLE_VALUE)
  1935.         {
  1936.            
  1937.         FindClose(hSearch);
  1938.           return; /* cannot do this directory */
  1939.  
  1940.         } /* no files */
  1941.  
  1942.    /* now check if file is subdir if so process if it is not "." or ".." */
  1943.       if ((strncmp(FileData.cFileName,".",1) != 0) &&
  1944.          (strncmp(FileData.cFileName,"..",2) != 0))
  1945.           {
  1946.             /* ok is not bs names */
  1947.              
  1948.              if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
  1949.                 {
  1950.               
  1951.                    if (SetCurrentDirectory(FileData.cFileName) == FALSE)
  1952.                       {
  1953.                        printf("TapeIO:--> Warning Couldn't change directory to %s \n",FileData.cFileName);
  1954.                       fflush(stdout);
  1955.                     }
  1956.                    else {
  1957.                           process_write_directory(SearchString,process_subdirs);
  1958.                           /* set current directory back */
  1959.                           SetCurrentDirectory("..");  
  1960.                          }
  1961.                  } /* is directory */
  1962.             } /* is not . or .. */
  1963.  
  1964.     /* when it returns recurse through remainder of subdirs */
  1965.     do
  1966.      {
  1967.        rvalue = FindNextFile(hSearch,&FileData);
  1968.        if ((rvalue == FALSE) && (GetLastError() == ERROR_NO_MORE_FILES))
  1969.          {
  1970.             done = TRUE;
  1971.             FindClose(hSearch);
  1972.          }  
  1973.  
  1974.        else
  1975.           {
  1976.           /* now check if file is subdir if so process if it is not "." or ".." */
  1977.             if ((strncmp(FileData.cFileName,".",1) != 0) &&
  1978.                 (strncmp(FileData.cFileName,"..",2) != 0))
  1979.               {
  1980.                 /* ok is not bs names */
  1981.                   /* instead of using getfileattrs dwFileAttributes */
  1982.  
  1983.                  if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
  1984.                      {
  1985.                         
  1986.                    /* is a directory so set directory and recurse */
  1987.                    if (SetCurrentDirectory(FileData.cFileName) == FALSE)
  1988.                      {
  1989.                       printf("TapeIO:--> Warning Couldn't change directory to %s \n",FileData.cFileName);
  1990.                       fflush(stdout);
  1991.                      }
  1992.                    else {
  1993.                           process_write_directory(SearchString,process_subdirs);
  1994.                            SetCurrentDirectory("..");
  1995.                          }
  1996.                   } /* is directory */
  1997.               } /* is not . or .. */
  1998.  
  1999.             } /* else not last file */
  2000.        } while (!done);
  2001.  
  2002.     } /* process subdirs */
  2003.   }
  2004.  
  2005.  
  2006.  
  2007. /* ------------------------------------------------------------------------------------ */
  2008.   void space_sequential_filemarks(no_marks)
  2009.        DWORD no_marks;
  2010.    {
  2011.      DWORD rvalue;
  2012.      
  2013.      if (debug)
  2014.         {
  2015.          printf("Attempting to Space %d Seqential file marks\n",no_marks);
  2016.          fflush(stdout);
  2017.         }
  2018.       
  2019.           rvalue = SetTapePosition(hTape,TAPE_SPACE_SEQUENTIAL_FMKS,0,no_marks,0,FALSE); 
  2020.           if (rvalue != NO_ERROR)
  2021.             report_error(" Tape positioning by sequential filemarks error ",rvalue,severe);
  2022.         
  2023.     }
  2024. /* ------------------------------------------------------------------------------------ */
  2025.     void space_scsi_blocks(no_blocks)
  2026.      DWORD no_blocks;
  2027.    {
  2028.      DWORD rvalue;
  2029.         
  2030.         if (debug)
  2031.           {
  2032.             printf("Attempting to Space %d Scsi data blocks \n",no_blocks);
  2033.             fflush(stdout);
  2034.           }
  2035.        
  2036.          rvalue = SetTapePosition(hTape,TAPE_SPACE_RELATIVE_BLOCKS,0,no_blocks,0,FALSE); 
  2037.           if (rvalue != NO_ERROR)
  2038.             report_error(" Tape positioning by scsi (relative) block error ",rvalue,severe);
  2039.         
  2040.     } 
  2041.   /* ---------------------------------------------------------------------------------- */
  2042.  
  2043.    void eject_tape()
  2044.    {
  2045.       DWORD rvalue;
  2046.  
  2047.        rvalue = PrepareTape(hTape,TAPE_UNLOAD,FALSE);
  2048.     
  2049.      if (rvalue != NO_ERROR)
  2050.         {
  2051.            report_error("Ejecting tape ",rvalue,severe);
  2052.         }
  2053.     else
  2054.         {
  2055.           if (debug)
  2056.             {
  2057.               printf("TapeIO:-->Unloaded/Ejected Tape \n");
  2058.               fflush(stdout);
  2059.              }
  2060.           }
  2061.   }
  2062.  
  2063. /*   ---------------------------------------------------------------------------------- */
  2064.  void list_tape()
  2065.    
  2066.    {
  2067.     
  2068.      int element_number = 0;
  2069.      
  2070.       read_tape_index();
  2071.          printf("Number      Saveset Name   Original Size   Compressed Size   Date \n");
  2072.       printf("__________________________________________________________________\n");
  2073.       for (element_number = 0; element_number < max_save_sets; element_number++)
  2074.           dump_header_element(element_number);
  2075.       printf("\nListing Complete\n\n");
  2076.       fflush(stdout);
  2077.  
  2078.     
  2079.    }
  2080.  
  2081. /* ------------------------------------------------------------------------------------ */
  2082.  int find_free_saveset_element()
  2083.  
  2084.   {
  2085.     BOOLEAN found = FALSE;
  2086.     int number = 0;
  2087.     int element_number = 0;
  2088.  
  2089.     for (element_number = 0; element_number < max_save_sets; element_number++)
  2090.        {
  2091.         if (strncmp(raw_tape_header.header[element_number].saveset_name,"EMPTY001_____",13) == 0)
  2092.           {
  2093.             number = element_number;
  2094.             found = TRUE;
  2095.             break;
  2096.           }
  2097.         }
  2098.        if (found)
  2099.          return(number);
  2100.        else return(-1); 
  2101.  
  2102.     }
  2103.  
  2104.  /* ------------------------------------------------------------------------------------- */
  2105.  BOOLEAN get_index_file_entry(indexrec)
  2106.    INDEX_REC_TYPE *indexrec;
  2107.    {
  2108.   
  2109.      int lsize;
  2110.      int csize;
  2111.      int sblock;
  2112.      char ldate[9];
  2113.      char fname[351];
  2114.      int rvalue;
  2115.  
  2116.      rvalue = fscanf(TempIndexFile,"%350s %10d %10d %8s %10d \n",fname,&lsize,&csize,ldate,&sblock);
  2117.       if (rvalue == 0)
  2118.          report_error("Reading index file entry \n",0,severe);
  2119.  
  2120.       if (rvalue == EOF)
  2121.         {
  2122.          if (debug)
  2123.            {
  2124.              printf("EOF of index file found \n");
  2125.              fflush(stdout);
  2126.             }
  2127.           fclose(TempIndexFile);
  2128.           return(FALSE);
  2129.          }
  2130.  
  2131.       if (debug)
  2132.         {
  2133.           printf("Successfully retrieved index entry %s %d %d %8s %d\n",fname,lsize,csize,ldate,sblock);
  2134.           fflush(stdout);
  2135.         }
  2136.  
  2137.      /* fill in fields */
  2138.      indexrec->original_size = lsize;
  2139.      indexrec->compressed_size = csize;
  2140.      strcpy(indexrec->fname,fname);
  2141.      strcpy(indexrec->date,ldate);
  2142.      indexrec->start_block = sblock;
  2143.  
  2144.        if (debug)
  2145.         {
  2146.           printf("Successfully filled in index entry %s %d %d %8s %d\n",
  2147.           indexrec->fname,
  2148.           indexrec->original_size,
  2149.           indexrec->compressed_size,
  2150.           indexrec->date,
  2151.           indexrec->start_block);
  2152.           fflush(stdout);
  2153.         }
  2154.        return(TRUE);
  2155.      
  2156.      }
  2157.  
  2158.  
  2159.  /* -------------------------------------------------------------------------------------- */
  2160.  
  2161.  void list_index_file(indexfilename2,savesetname2)
  2162.    char *indexfilename2;
  2163.    char *savesetname2;
  2164.    {
  2165.  
  2166.  
  2167.      INDEX_REC_TYPE indexrec;  
  2168.      BOOLEAN rvalue = FALSE;
  2169.      
  2170.  
  2171.        /* print header */
  2172.       printf("Saveset Listing for %s \n",savesetname2);
  2173.       printf("Original Size    Compressed Size   Date   Start Block  Filename\n");
  2174.       printf("__________________________________________________________________________\n");
  2175.      
  2176.       /* loop through and get file entry and print out contents */
  2177.       /* first open up the index file for reading */
  2178.                        
  2179.        TempIndexFile = fopen(indexfilename2,"r");
  2180.            
  2181.        if (TempIndexFile == NULL)
  2182.           report_error(" Could not open temporary index file.",0,severe);
  2183.        else 
  2184.         {
  2185.           if (debug)
  2186.             {
  2187.               printf("opened for read temp index file %s \n",indexfilename2);
  2188.               fflush(stdout);
  2189.             }
  2190.  
  2191.            tempindexfileopen = TRUE;
  2192.          }
  2193.           
  2194.       do {
  2195.            rvalue = get_index_file_entry(&indexrec);
  2196.          if (rvalue)
  2197.            {
  2198.                  printf("%10d          %10d  %8s   %10d  %-s\n",
  2199.                    indexrec.original_size,
  2200.                    indexrec.compressed_size,
  2201.                     indexrec.date,
  2202.                     indexrec.start_block,
  2203.                     indexrec.fname);
  2204.                fflush(stdout);
  2205.             }
  2206.  
  2207.          } while (rvalue == TRUE);
  2208.  
  2209.       printf("\nListing Complete\n\n");
  2210.       fflush(stdout);
  2211.   }
  2212.  
  2213. /* ------------------------------------------------------------------------------------ */
  2214.  int find_saveset_element(saveset)
  2215.   char saveset[18];
  2216.  
  2217.   {
  2218.     BOOLEAN found = FALSE;
  2219.     int number = 0;
  2220.     int element_number = 0;
  2221.     int slen = strlen(saveset);
  2222.  
  2223.     for (element_number = 0; element_number < max_save_sets; element_number++)
  2224.        {
  2225.         if (strncmp(raw_tape_header.header[element_number].saveset_name,saveset,slen) == 0)
  2226.           {
  2227.             number = element_number;
  2228.             found = TRUE;
  2229.             break;
  2230.           }
  2231.         }
  2232.        if (found)
  2233.          return(number);
  2234.        else return(-1); 
  2235.  
  2236.     }
  2237.  
  2238. /* --------------------------------------------------------------------------------------*/
  2239.  void list_saveset(saveset)
  2240.    char *saveset[18];
  2241.    {
  2242.      int element_number;
  2243.      char indexfilename[400];
  2244.      
  2245.        read_tape_index();
  2246.      rewind_tape();
  2247.      element_number = find_saveset_element(saveset);
  2248.  
  2249.      if (element_number < 0)
  2250.            {
  2251.              printf("Saveset %s Not found on the tape, use LKBACKUP -l for a listing. \n",saveset);
  2252.           fflush(stdout);
  2253.              close_tape();
  2254.              report_error(" ",0,severe);
  2255.         }
  2256.  
  2257.       /* now that we found the saveset space to the directory listing and read it */
  2258.        /* tape id 1 
  2259.           1 data 2 index 3 saveset 0
  2260.           3 data 4 index 5 saveset 1
  2261.           5 data 6 index 7 saveset 2
  2262.           7 data 8 index 9 saveset 3
  2263.                .
  2264.             .
  2265.             .
  2266.             tape index [filemark]
  2267.  
  2268.           etc. */
  2269.       space_filemarks((element_number * 2) + 2);
  2270.       /* now recreate the file and list it */
  2271.       restore_file(indexfilename,0,0,TRUE);
  2272.      
  2273.       list_index_file(indexfilename,saveset);
  2274.       DeleteFile(indexfilename);
  2275.  
  2276.       rewind_tape();
  2277.       close_tape();
  2278.  
  2279.     }
  2280.    
  2281.    /* --------------------------------------------------------------------------------- */
  2282.  void process_restore(indexfilename2,search_string)
  2283.    char *indexfilename2;
  2284.    char search_string[];
  2285.   
  2286.   {
  2287.    
  2288.    /* now go through each line in the index file and check if name matches */
  2289.    
  2290.         int lsize;
  2291.      int csize;
  2292.      int sblock;
  2293.      char ldate[9];
  2294.      char fname[351];
  2295.      int rvalue;
  2296.      BOOLEAN eof_found = FALSE;
  2297.      INDEX_REC_TYPE indexrec;
  2298.  
  2299.  
  2300.        /* first open up the index file for reading */
  2301.                        
  2302.        TempIndexFile = fopen(indexfilename2,"r");
  2303.            
  2304.        if (TempIndexFile == NULL)
  2305.           report_error(" Could not open temporary index file.",0,severe);
  2306.        else 
  2307.         {
  2308.           if (debug)
  2309.             {
  2310.               printf("opened for read temp index file %s \n",indexfilename2);
  2311.               fflush(stdout);
  2312.             }
  2313.  
  2314.            tempindexfileopen = TRUE;
  2315.          }
  2316.        
  2317.     do {
  2318.  
  2319.       rvalue = fscanf(TempIndexFile,"%350s %10d %10d %8s %10d \n",fname,&lsize,&csize,ldate,&sblock);
  2320.       if (rvalue == 0)
  2321.          report_error("Reading index file entry \n",0,severe);
  2322.  
  2323.       if (rvalue == EOF)
  2324.         {
  2325.          if (debug)
  2326.            {
  2327.              printf("EOF of index file found \n");
  2328.              fflush(stdout);
  2329.             }
  2330.           fclose(TempIndexFile);
  2331.           eof_found = TRUE;
  2332.          }
  2333.        else
  2334.         {
  2335.           if (debug)
  2336.            {
  2337.           printf("Successfully retrieved index entry %s %d %d %8s %d\n",fname,lsize,csize,ldate,sblock);
  2338.           fflush(stdout);
  2339.            }
  2340.  
  2341.      /* fill in fields */
  2342.      indexrec.original_size = lsize;
  2343.      indexrec.compressed_size = csize;
  2344.      strcpy(indexrec.fname,fname);
  2345.      strcpy(indexrec.date,ldate);
  2346.      indexrec.start_block = sblock;
  2347.  
  2348.        if (debug)
  2349.         {
  2350.           printf("Successfully filled in index entry %s %d %d %8s %d\n",
  2351.           indexrec.fname,
  2352.           indexrec.original_size,
  2353.           indexrec.compressed_size,
  2354.           indexrec.date,
  2355.           indexrec.start_block);
  2356.           fflush(stdout);
  2357.         }
  2358.       /* now check for match */
  2359.  
  2360.       if (wild_match(search_string,indexrec.fname))
  2361.          {
  2362.            if (debug)
  2363.              {
  2364.                printf("match found %s with %s \n",search_string,indexrec.fname);
  2365.                printf("current block = %d need to be at block %d \n",current_tape_block_number,
  2366.                        indexrec.start_block);
  2367.                fflush(stdout);
  2368.              }
  2369.             if (current_tape_block_number < indexrec.start_block)
  2370.               {
  2371.                 if (debug)
  2372.                   {
  2373.                   printf("attempting to space forward %d blocks \n",
  2374.                    indexrec.start_block - current_tape_block_number);
  2375.                   fflush(stdout);
  2376.                   }
  2377.                  space_scsi_blocks(indexrec.start_block - current_tape_block_number);
  2378.                  current_tape_block_number = indexrec.start_block;
  2379.                }
  2380.              else if (current_tape_block_number > indexrec.start_block)
  2381.                 {
  2382.                   /* error condition */
  2383.                   printf("TapeIO --->: Error tape position of %d > start block of %d shouldn't happen \n",
  2384.                              current_tape_block_number,indexrec.start_block);
  2385.                   fflush(stdout);
  2386.                   report_error(" ",0,severe);
  2387.                 }
  2388.                /* now restore the file */
  2389.                restore_file(indexrec.fname,indexrec.original_size,indexrec.compressed_size,FALSE);
  2390.           
  2391.            } /* end of ok match */
  2392.  
  2393.       } /* eof not found */
  2394.  
  2395.     } while (!eof_found);
  2396.  
  2397.   }   
  2398.   /* --------------------------------------------------------------------------------------*/
  2399.  
  2400.  void do_restore(saveset,search_string)
  2401.    char saveset[18];
  2402.    char search_string[];
  2403.    {
  2404.      int element_number;
  2405.      char indexfilename[400];
  2406.      
  2407.        store_start_time();
  2408.      total_bytes_read = 0;
  2409.      filescompressed = 0;
  2410.      filesuncompressed = 0;
  2411.  
  2412.      read_tape_index();
  2413.      rewind_tape();
  2414.      element_number = find_saveset_element(saveset);
  2415.  
  2416.      if (element_number < 0)
  2417.            {
  2418.              printf("Saveset %s Not found on the tape, use LKBACKUP -l for a listing. \n",saveset);
  2419.           fflush(stdout);
  2420.              close_tape();
  2421.              report_error(" ",0,severe);
  2422.         }
  2423.  
  2424.       /* now that we found the saveset space to the directory listing and read it */
  2425.        /* tape id 1 
  2426.           1 data 2 index 3 saveset 0
  2427.           3 data 4 index 5 saveset 1
  2428.           5 data 6 index 7 saveset 2
  2429.           7 data 8 index 9 saveset 3
  2430.                .
  2431.             .
  2432.             .
  2433.             tape index [filemark]
  2434.  
  2435.           etc. */
  2436.       space_filemarks((element_number * 2) + 2);
  2437.       /* now recreate the file and list it */
  2438.       restore_file(indexfilename,0,0,TRUE);
  2439.  
  2440.       /* now position to the backup set */
  2441.       rewind_tape();
  2442.       space_filemarks((element_number * 2) + 1);
  2443.       current_tape_block_number = 0;
  2444.        
  2445.       /* now process the restore */
  2446.       total_bytes_read = 0;
  2447.       filescompressed = 0;
  2448.       filesuncompressed = 0;
  2449.  
  2450.       process_restore(indexfilename,search_string);
  2451.       
  2452.       /* now finish up */
  2453.       DeleteFile(indexfilename);
  2454.  
  2455.       rewind_tape();
  2456.       close_tape();
  2457.  
  2458.       if ((filescompressed+filesuncompressed) <= 0)
  2459.         {
  2460.          printf("Warning no files in saveset %s found with search string %s \n",saveset,
  2461.                  search_string);
  2462.          fflush(stdout);
  2463.         }
  2464.       else print_restore_stats();
  2465.  
  2466.     }
  2467.  
  2468. /* ------------------------------------------------------------------------------------ */
  2469.  
  2470. void do_backup(saveset_name,search_string,do_subdirs)
  2471.   char saveset_name[];
  2472.   char search_string[];
  2473.   BOOLEAN do_subdirs;
  2474.   {
  2475.     int free_element = 0;
  2476.     int taken_element = 0;
  2477.     read_tape_index();
  2478.     rewind_tape();
  2479.  
  2480.     free_element = find_free_saveset_element();
  2481.     if (free_element < 0)
  2482.       report_error("No room for any more savesets on tape, initialize it or switch tapes \n",0,severe);
  2483.     
  2484.      taken_element = find_saveset_element(saveset_name);
  2485.      if (taken_element >= 0)
  2486.            {
  2487.              printf("Error: Saveset %s Already exists on the tape... Try another name. \n",saveset_name);
  2488.           fflush(stdout);
  2489.              report_error(" ",0,severe);
  2490.         }
  2491.     else
  2492.       {
  2493.         store_start_time();
  2494.         /* now space to the correct file mark and start writing the backup */
  2495.            space_filemarks((free_element * 2) + 1 ); // 1 extra to get over header */
  2496.             /* reset the current tape block number to be relative from the beginning of the
  2497.               save set */
  2498.             current_tape_block_number = 0;
  2499.  
  2500.             /* this should put the tape ready to write the backup since there is two file marks
  2501.             per backup 1 before the backup and 1 between the backup and its directory so the
  2502.             directory can be retrieved quickly ie element 0 is filemark 0 element4 is file mark
  2503.             8 */
  2504.          
  2505.         /* now do the backup */
  2506.         total_original_size   = 0;
  2507.         total_backup_size = 0;
  2508.         filescompressed = 0;
  2509.         filesuncompressed = 0;
  2510.  
  2511.         process_write_directory(search_string,do_subdirs); /* also do subdirectories if set */
  2512.          
  2513.         /* dont write marks if nothing was backed up */
  2514.         if ((filescompressed + filesuncompressed) == 0)
  2515.            {
  2516.              printf("Warning no files found in search parameter %s \n",search_string);
  2517.              fflush(stdout);
  2518.            }
  2519.         else
  2520.            {
  2521.               /* now write the file mark */
  2522.              write_filemarks(1);
  2523.              /* now write the directory file */
  2524.              write_directory_file();
  2525.              /* now write another file mark */
  2526.              write_filemarks(1);
  2527.  
  2528.             /* now fill in the total size/compressed size and rewind and rewrite header */
  2529.             /* also print out status messages */
  2530.             fill_in_header_element(saveset_name,total_original_size,total_backup_size,
  2531.                     todays_date,free_element);
  2532.             /* now rewrite header */
  2533.             write_tape_index();
  2534.            }
  2535.         
  2536.         /* now rewind tape */
  2537.         rewind_tape();
  2538.         close_tape();
  2539.         print_backup_stats();
  2540.  
  2541.    } /* is room for new saveset */
  2542.  }
  2543. /* ------------------------------------------------------------------------------------ */
  2544.  void disp_help()
  2545.    {
  2546.      printf("             USAGE for LKBACKUP Version 1.0 by L. Kahn 1993           \n");
  2547.      printf("LKBACKUP [-d -q -c -s] -b saveset_name search_string [include_subdirs] \n");
  2548.      printf("LKBACKUP [-d -q -c] -e [long_or_short] or LGKBACKUP -h or    \n");
  2549.      printf("LKBACKUP [-d -q -c -s] -i or\n");
  2550.      printf("LKBACKUP [-d -q -c -s] -L saveset_name or\n");  
  2551.      printf("LKBACKUP [-d -q -c -s] -l or                \n");
  2552.      printf("LKBACKUP [-d -q -c -s] -r saveset_name search_string \n");
  2553.      printf("LKBACKUP [-d -q -c -s] -C no_of_good_savesets\n");
  2554.      printf("Where ... \n");
  2555.      printf("-b = Backup using the search string starting from the current directory, \n"); 
  2556.      printf("     including subdirs is optional and depedent on the value of \n");
  2557.      printf("     include_subddirs (T or F) \n");
  2558.      printf("-e = Erase tape where erasing long or short is optional. Default is short \n");
  2559.      printf("-h = Display this HELP message \n");
  2560.      printf("-i = Initialize tape (necessary before usage) \n");
  2561.      printf("-l = List tape (i.e., name of all savesets)\n");
  2562.      printf("-L = List the contents of a specific saveset \n"); 
  2563.      printf("-r = Restore the saveset_name using the search_string\n");
  2564.      printf("-C = Catalog tape (set index to say there are no_good_savesets)  \n");
  2565.      printf("     Should be used if for some reason a backup fails or power goes out\n");
  2566.      printf("     during a backup. Rewrites the tape index, restores can then be \n");
  2567.      printf("     performed but further backups may be unreliable.\n");
  2568.      printf("-d = turn on debugging, -q = quiet_mode, -c = turn on compression -s use setmarks not filemarks");
  2569.    
  2570.  }
  2571.  
  2572. /* ------------------------- main program */
  2573.   
  2574.   void main(int argc,char *argv[])
  2575.   { 
  2576.   
  2577.       int current_arg = 1;
  2578.       BOOLEAN more_args = TRUE;
  2579.       BOOLEAN done = FALSE;
  2580.       BOOLEAN do_subdirs = FALSE;
  2581.      
  2582.  
  2583.        fill_in_todays_date();
  2584.       
  2585.       if ((argc < 2) || (argc > 8)) 
  2586.          disp_help();
  2587.        else
  2588.          
  2589.          {
  2590.            /* process arguments */
  2591.             if (argv[current_arg][0] != '-')
  2592.               disp_help();
  2593.            else
  2594.         
  2595.             do 
  2596.               {
  2597.            /* get the first one and if it doesn't start with a - we have a problem */
  2598.         
  2599.               switch (argv[current_arg][1])
  2600.                 {
  2601.                 case 'c':
  2602.                   compress = TRUE;
  2603.                   break;
  2604.                 
  2605.                 case 'q':
  2606.                   quiet_mode = TRUE;
  2607.                   break;
  2608.                 
  2609.                 case 'd':
  2610.                   debug = TRUE;
  2611.                   break;
  2612.  
  2613.                 case 'h':
  2614.                   disp_help();
  2615.                   done = TRUE;
  2616.                   break;
  2617.  
  2618.                  case 's':
  2619.                    set_mark_positioning = TRUE;
  2620.                    break;
  2621.  
  2622.                   case 'C':
  2623.                   if (argc < (current_arg + 2))
  2624.                     {
  2625.                      printf("ERROR: Number of good savesets needed for catalog procedure \n");
  2626.                      fflush(stdout);
  2627.                     }
  2628.                    else {
  2629.                          catalog_tape(argv[current_arg + 1]);
  2630.                           }
  2631.  
  2632.                    done = TRUE;
  2633.                    break;
  2634.             
  2635.                 case 'i':
  2636.                    initialize_tape();
  2637.                    done = TRUE;
  2638.                    break;
  2639.                 
  2640.                 case 'l':
  2641.                    list_tape();
  2642.                    done = TRUE;
  2643.                    break;
  2644.                 
  2645.                 case 'L':
  2646.                   if (argc < (current_arg + 2))
  2647.                     {
  2648.                      printf("ERROR: Saveset name needed for tape listing. Use LKBACKUP -h for help \n");
  2649.                      fflush(stdout);
  2650.                     }
  2651.                    else {
  2652.                          list_saveset(argv[current_arg + 1]);
  2653.                           }
  2654.  
  2655.                    done = TRUE;
  2656.                    break;
  2657.                  
  2658.                  case 'e':
  2659.                      if (argc >= (current_arg + 2))
  2660.                       {
  2661.                      /* we have a short or long option so check it */
  2662.                      switch (argv[current_arg+1][0])
  2663.                        {
  2664.                          case 's':
  2665.                           erase_tape(FALSE);
  2666.                           done = TRUE;
  2667.                           break;
  2668.  
  2669.                          case 'l':
  2670.                            erase_tape(TRUE);
  2671.                            done = TRUE;
  2672.                            break;
  2673.                          
  2674.                          default:
  2675.                           printf("ERROR: Legal options for erase are s for short erase or l for long erase \n");
  2676.                           printf("Use LKBACKUP -h for help \n");
  2677.                           fflush(stdout);
  2678.                           done = TRUE;
  2679.                           break;
  2680.                          }
  2681.                          if (done)
  2682.                            break;
  2683.                         } /* end have extra arg */
  2684.  
  2685.                       else erase_tape(FALSE);
  2686.                      done = TRUE;
  2687.                      break;
  2688.   
  2689.                  case 'b':
  2690.                      /* make sure we have enough parms */
  2691.                      if (argc < (current_arg+3)) 
  2692.                          {
  2693.                           printf("ERROR: The search_string and the saveset_name are required for a backup operation. \n");
  2694.                           printf("Use LKBACKUP -h for help \n");
  2695.                           fflush(stdout);
  2696.                           done = TRUE;
  2697.                           break;
  2698.                          }
  2699.  
  2700.                       /* check for include subdirs */
  2701.                     if (argc == (current_arg + 4))
  2702.                        {
  2703.                        
  2704.                        switch (argv[current_arg+3][0])
  2705.                          {
  2706.                           case 't': 
  2707.                             do_subdirs = TRUE;
  2708.                             break;
  2709.                           case 'f':
  2710.                             do_subdirs = FALSE;
  2711.                             break;
  2712.                           default:
  2713.                              printf("Error: The legal options for including subdirectories are t for f.\n");
  2714.                             printf("Use LKBACKUP -h for help \n");
  2715.                             fflush(stdout);
  2716.                             done = TRUE;
  2717.                             break;
  2718.                           }
  2719.                        } /* have optional parm */
  2720.                       if (done)
  2721.                         break;
  2722.  
  2723.                      /* if we get here do the backup */
  2724.                      if (debug)
  2725.                        {
  2726.                         printf("calling backup %s %s %d \n",argv[current_arg+1],
  2727.                                   argv[current_arg+2],do_subdirs);
  2728.                         fflush(stdout);
  2729.                         }
  2730.                        do_backup(argv[current_arg+1],argv[current_arg+2],do_subdirs);
  2731.                        done = TRUE;
  2732.                        break;
  2733.  
  2734.                  case 'r':
  2735.                                    /* make sure we have enough parms */
  2736.                      if (argc < (current_arg+3)) 
  2737.                          {
  2738.                           printf("ERROR: The search_string and the saveset_name are required for a restore operation. \n");                          printf("Use LKBACKUP -h for help \n");
  2739.                           fflush(stdout);
  2740.                           done = TRUE;
  2741.                           break;
  2742.                          }
  2743.                        
  2744.                        else
  2745.                         {
  2746.                           if (debug)
  2747.                             {
  2748.                              printf("Restore %s %s \n",argv[current_arg+1],argv[current_arg+2]);
  2749.                              fflush(stdout);
  2750.                             }
  2751.  
  2752.                          do_restore(argv[current_arg+1],argv[current_arg+2]);
  2753.                          done = TRUE;
  2754.                          break;
  2755.                         }
  2756.  
  2757.                  default:
  2758.                   printf("Illegal option %s \n",argv[current_arg]);
  2759.                   disp_help();
  2760.                   done = TRUE;
  2761.                   break;
  2762.  
  2763.                 } /* end of switch */
  2764.                
  2765.                ++current_arg;
  2766.                if (current_arg > argc)
  2767.                  more_args = FALSE;
  2768.                    
  2769.               } while ((!done) && (more_args));
  2770.  
  2771.     } /* end of ok case */    
  2772.   } /* end of main */
  2773.                    
  2774.  
  2775.   
  2776.